import storeUtil from "@pangza-club/utils/store";
import { Link, useNavigate } from "react-router-dom";
import UserHeader from '@/assets/image/logo.png'
import { Avatar, Dropdown, MenuProps, Modal, message, } from "antd";
import { userStore } from "@/store/user";
import { doLogout } from "@/api/user";
const UserInfo = () => {
    const useUserStore = userStore();
    const user = storeUtil.get('user');
    const nav = useNavigate();
    const items: MenuProps['items'] = [
        {
            label: (
                <Link target="_blank" rel="noopener noreferrer" to="/user_info">
                    个人中心
                </Link>
            ),
            key: '0',
        },
        {
            label: (
                <span>
                    退出登录
                </span>
            ),
            onClick: () => {
                Modal.confirm({
                    title: '系统提示',
                    content: '确定退出登录吗？',
                    onOk: () => {
                        doLogout().then(() => {
                            useUserStore.reset();
                            message.success('退出成功');
                            nav('/login', {replace: true});
                        })
                    }
                })
            },
            key: '1',
        }
    ];
    return <div>
        {
            user?.token ? <Dropdown trigger={['click']} menu={{ items }}>
                <div className="cursor-pointer">
                    <Avatar src={user?.avatar || UserHeader} /> {user?.nickName}
                </div>
            </Dropdown> :
                <Link to='/login'>
                    去登录
                </Link>
        }
    </div>
}

export default UserInfo;